<?php
class IndexController extends Controller
{
    public $models = 'Blog,Auth,Tag,Setting';
    
    public function index()
    {
        global $PARAM, $ENV;//index.pagesize
        $blogs = null;
        $totalItems = $this->Blog->totalItems();
        if (!empty($PARAM['tag'])){
            $tag = $this->Tag->getTagByName(htmlspecialchars($PARAM['tag']));
            $tag && ($totalItems = $this->Tag->countBlogByTagIds(array($tag['id'])));
        } elseif (!empty($PARAM['keyword']))
            $totalItems = $this->Blog->countBlogByKeyword(sqlFilter($PARAM['keyword']));
        $totalPage = ceil($totalItems / $ENV['index.pagesize']);
        $page = isset($PARAM['page']) ? (int)$PARAM['page'] - 1 : 0;
        $page = ($page < 0 ) ? 0 : $page;
        $page = ($page + 1 > $totalPage) ? ($totalPage - 1) : $page;
        $offset = $page * $ENV['index.pagesize'];
        $offset = $offset < 0 ? 0 : $offset;
        $keyword = isset($PARAM['keyword']) ? trim($PARAM['keyword']) : null;
        if (!empty($tag))
            $blogs = $this->Blog->getBlogsByTagId($tag['id'], $offset, $ENV['index.pagesize']);
        if ($keyword){
            $blogs = array();
            if ($PARAM['flag'] == 'blog')
                $blogs = $this->Blog->getBlogsByKeyword(sqlFilter($keyword), $offset, $ENV['index.pagesize']);
//            else if ($PARAM['flag'] == 'tag')
//                $blogs = $this->Blog->getBlogsByKeyword(sqlFilter($keyword), $offset, $ENV['index.pagesize']);
        }
        if (is_null($blogs))
            $blogs = $this->Blog->getBlogs($offset, $ENV['index.pagesize']);
        $tagIds = array();
        foreach ($blogs as $k => $b){
            if ($b['tags']){
                $ids = arrayTrim(explode('#', $b['tags']));
                $blogs[$k]['tags'] = $ids;
                $tagIds = array_merge($tagIds, $ids);
            }
        }
        $tagIds = array_unique($tagIds);
        $tags = empty($tagIds)? null : $this->Tag->getTagsByIds($tagIds);
        //$topTags = $this->Tag->getAll('counts DESC');//getTopTags(16);
        $topTags = $this->Tag->getTopTags(30);
        $params = array('curPage' => ($page+1), 'totalPage' => $totalPage);
        if (isset($PARAM['tag']))
            $params['tag'] = $PARAM['tag'];
        elseif ($keyword){
            $params['keyword'] = $PARAM['keyword'];
            $params['flag'] = $PARAM['flag'];
        }
        $this->render(array('title' => _('首页'), 'blogs' => $blogs, 'tags' => $tags, 'topTags' => $topTags, 'params' => $params), 'index');
    }

    public function login()
    {
        global $PARAM;
        if ($this->Auth->isLogin())
            redirect('index');
        if (!isset($PARAM['submit']))
            $this->render() && die();
        if ($this->Auth->login($PARAM['username'], $PARAM['password'])){
            $home = $this->Setting->getConfig('homePage');
            if (!empty($home)){
                header("Location: $home");
                die();
            }
            redirect('index');
        }
        $this->Auth->logout();
        $this->render(array('msg' => _('密码错误')));
    }

    public function logout()
    {
        $this->Auth->logout();
        redirect('index', 'login');
    }
}
?>
